package com.ynsj.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ynsj.domain.SysMenu;
import com.ynsj.domain.SysPrivilege;
import com.ynsj.model.RolePrivilegesParam;
import com.ynsj.service.SysMenuService;
import com.ynsj.service.SysPrivilegeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ynsj.mapper.SysRolePrivilegeMapper;
import com.ynsj.domain.SysRolePrivilege;
import com.ynsj.service.SysRolePrivilegeService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

@Service
public class SysRolePrivilegeServiceImpl extends ServiceImpl<SysRolePrivilegeMapper, SysRolePrivilege> implements SysRolePrivilegeService{

    @Autowired
    private SysMenuService sysMenuService;

    @Autowired
    private SysPrivilegeService sysPrivilegeService;

    @Autowired
    private SysRolePrivilegeService sysRolePrivilegeService;

    @Override
    public List<SysMenu> findSysMenuAndPrivileges(Long roleId) {
        //查询出所有菜单
        List<SysMenu> sysMenus = sysMenuService.list();
        if (CollectionUtils.isEmpty(sysMenus)){
            return Collections.emptyList();
        }
        //提取出所有一级菜单
        List<SysMenu> rootMenus = sysMenus.stream().filter(sysMenu -> sysMenu.getParentId() == null).collect(Collectors.toList());
        if (CollectionUtils.isEmpty(rootMenus)){
            return Collections.emptyList();
        }
        //查询所有二级菜单
        List<SysMenu> subMenu = new ArrayList<>();
        rootMenus.forEach(rootMenu->
                subMenu.addAll(getChildMenus(rootMenu.getId(),roleId,sysMenus)));
        return subMenu;
    }

    @Transactional
    @Override
    public boolean grantPrivileges(RolePrivilegesParam rolePrivilegesParam) {
        //先删除该角色之前的权限
        sysRolePrivilegeService.remove(new LambdaQueryWrapper<SysRolePrivilege>().eq(SysRolePrivilege::getRoleId,rolePrivilegesParam.getRoleId()));
        List<Long> privilegeIds = rolePrivilegesParam.getPrivilegeIds();
        if (!CollectionUtils.isEmpty(privilegeIds)){
            List<SysRolePrivilege> sysRolePrivileges = new ArrayList<>();
            privilegeIds.forEach(privilegeId -> {
                SysRolePrivilege sysRolePrivilege = new SysRolePrivilege();
                sysRolePrivilege.setPrivilegeId(privilegeId);
                sysRolePrivilege.setRoleId(rolePrivilegesParam.getRoleId());
                sysRolePrivileges.add(sysRolePrivilege);
            });
            //新增选中的值
            return sysRolePrivilegeService.saveBatch(sysRolePrivileges);
        }
        return true;
    }

    /**
     *
     * @param parentId 父菜单的id
     * @param roleId 当前查询的角色id
     * @param sources 查询数据源
     * @return
     */
    private List<SysMenu> getChildMenus(Long parentId, Long roleId, List<SysMenu> sources) {
        List<SysMenu> childes = new ArrayList<>();
        sources.forEach(sysMenu -> {
            if (Objects.equals(sysMenu.getParentId(), parentId)){
                childes.add(sysMenu);
                //给子菜单设置子菜单
                sysMenu.setChild(getChildMenus(sysMenu.getId(),roleId,sources));
                //查询子菜单的权限
                sysMenu.setPrivileges(sysPrivilegeService.getAllSysPrivileges(roleId,sysMenu.getId()));
            }
         });
        return childes;
    }
}
